package com.younion.action.manage;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import com.younion.business.IActiveService;
import com.younion.business.IAdvertisingService;
import com.younion.business.ICardEntityService;
import com.younion.business.ICustomerService;
import com.younion.business.IFavourableEntityService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.SysConstants;
import com.younion.common.paginator.Paginator;
import com.younion.common.util.ImageFileUtil;
import com.younion.vo.AdvertisingVO;
import com.younion.vo.UserVO;

import cpcns.jhsks.util.MapCollectionUtils;

public class AdvertisingAction extends BaseAction {
	
	private static final long serialVersionUID = -4742341166877230011L;

	protected static final Log logger = LogFactory.getLog(AdvertisingAction.class);
	
	private IAdvertisingService advertisingService = null;
	
	@Autowired
	private ICardEntityService cardEntityService = null;
	@Autowired
	private IActiveService activeService = null;
	@Autowired
	private ICustomerService customerService = null;
	@Autowired
	private IFavourableEntityService favourableEntityService = null;
	
	private AdvertisingVO advertisingVO = new AdvertisingVO();
	
	private List<AdvertisingVO> advertisingList;
	
	private String ids;
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listAdvertising")
	public String listAdvertising() {
		try {
			Map paramMap = null;
			if (this.advertisingVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(this.advertisingVO));
			}
			// 获取总记录数
			int total = this.advertisingService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			this.advertisingList = this.advertisingService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(2, "广告列表", "查询广告列表");
		return SUCCESS;
	}
	/**
	 * 新增
	 */
	@RequiresPermissions("addAdvertising")
	public String addAdvertising() {
		return SUCCESS;
	}
	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveAdvertising")
	public String saveAdvertising() {
		UserVO user = this.getSessionUser();
		try {
			if (this.advertisingVO.getId() == null) {
				this.advertisingVO.setAddUserId(user.getId());
				if (this.advertisingVO.getTop() == 1) {
					this.advertisingVO.setTop(this.advertisingService.selectMaxTop() + 1);
				}
				this.advertisingVO.setId(this.advertisingService.save(this.advertisingVO));
				this.addLog(3, "添加广告："+this.advertisingVO.getTitle(), "ID为"+this.advertisingVO.getId());
			} else {
				this.advertisingVO.setUpdateUserId(user.getId());
				Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(this.advertisingVO));
				this.advertisingService.updateAdvertisingByMap(paramMap);
				this.addLog(4, "修改广告："+this.advertisingVO.getTitle(), "ID为"+this.advertisingVO.getId());
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		ImageFileUtil.processImageFiles(this.advertisingVO.getId(), "1", request);
		request.setAttribute("refreshNavTabId", "advertisingList");
		return SUCCESS;
	}
	/**
	 *修改保存，调度Jsp
	 */
	@RequiresPermissions("editAdvertising")
	public String editAdvertising() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("id", this.advertisingVO.getId());
		try {
			this.advertisingVO = this.advertisingService.selectAdvertisingById(map);
			String tempVal = null;
			System.out.println(this.advertisingVO.getLinkType()+":::"+this.advertisingVO.getLinkId()+":::"+this.cardEntityService);
			switch (this.advertisingVO.getLinkType()) {
				case 0 : 
					tempVal = this.cardEntityService.getById(String.valueOf(this.advertisingVO.getLinkId())).getCardName();
					break;
				case 1 : 
					tempVal = this.favourableEntityService.getById(String.valueOf(this.advertisingVO.getLinkId())).getName();
					break;
				case 2 : 
					tempVal = this.customerService.getById(String.valueOf(this.advertisingVO.getLinkId())).getName();
					break;
				case 3 : 
					tempVal = this.activeService.getById(String.valueOf(this.advertisingVO.getLinkId())).getTitle();
					break;
				case 4 : 
					tempVal = this.advertisingVO.getLinkUrl();
					break;
			}
			request.setAttribute("tempVal", tempVal);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 */
	@RequiresPermissions("deleteAdvertising")
	public String deleteAdvertising() {
		String realPath = request.getSession().getServletContext().getRealPath(SysConstants.ADVERTISING_IMG_PATH);
		File file = new File(realPath);
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("id", this.advertisingVO.getId());
		try {
			AdvertisingVO vo = this.advertisingService.selectAdvertisingById(map);
			this.advertisingService.deleteAdvertisingById(map);
			File toFile = new File(file, String.valueOf(this.advertisingVO.getId()));
			ImageFileUtil.deleteImage(toFile, vo.getPicNames(), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(5, "删除广告", "ID为"+this.advertisingVO.getId());
		request.setAttribute("refreshNavTabId", "advertisingList");
		return SUCCESS;
	}
	/**
	 * 批量删除
	 */
	@RequiresPermissions("deleteAdvertisings")
	public String deleteAdvertisings() {
		String realPath = request.getSession().getServletContext().getRealPath(SysConstants.ADVERTISING_IMG_PATH);
		List list = Arrays.asList(this.ids.split(","));
		File file = new File(realPath);
		File toFile;
		try {
			List<AdvertisingVO> listVo = this.advertisingService.selectByIds(list);
			for (AdvertisingVO vo : listVo) {
				toFile = new File(file, String.valueOf(vo.getId()));
				ImageFileUtil.deleteImage(toFile, vo.getPicNames(), true);
			}
			this.advertisingService.deleteAdvertisingByIds(list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(5, "批量删除广告", "ID为"+this.ids);
		request.setAttribute("refreshNavTabId", "advertisingList");
		return SUCCESS;
	}
	
	@RequiresPermissions("updateTopAdvertising")
	public String updateTopAdvertising() {
		try {
			Map<String, Integer> map = new HashMap<String, Integer>();
			map.put("id", this.advertisingVO.getId());
			Integer maxTop = this.advertisingService.selectMaxTop();
			map.put("maxTop", maxTop + 1);
			this.advertisingService.updateTopById(map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(4, "置顶广告", "ID为"+this.advertisingVO.getId());
		request.setAttribute("refreshNavTabId", "advertisingList");
		return SUCCESS;
	}
	
	@RequiresPermissions("updateStateAdvertising")
	public String updateStateAdvertising() {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("state", this.advertisingVO.getState());
		List list = Arrays.asList(this.ids.split(","));
		map.put("list", list);
		try {
			this.advertisingService.updateStateByIds(map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(4, "修改广告状态", "ID为"+this.advertisingVO.getId());
		request.setAttribute("refreshNavTabId", "advertisingList");
		return SUCCESS;
	}
	
	public void setAdvertisingService(IAdvertisingService advertisingService) {
		this.advertisingService = advertisingService;
	}
	
	public List<AdvertisingVO> getAdvertisingList() {
		return this.advertisingList;
	}

	public AdvertisingVO getAdvertisingVO() {
		return advertisingVO;
	}

	public void setAdvertisingVO(AdvertisingVO advertisingVO) {
		this.advertisingVO = advertisingVO;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	public void setCardEntityService(ICardEntityService cardEntityService) {
		this.cardEntityService = cardEntityService;
	}
	public void setActiveService(IActiveService activeService) {
		this.activeService = activeService;
	}
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	public void setFavourableEntityService(
			IFavourableEntityService favourableEntityService) {
		this.favourableEntityService = favourableEntityService;
	}

}
